package com.jw.server.controller;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import cn.ydx.protobuf.OTIpcDef;
import cn.ydx.protobuf.OTIpcDef.OTIpcList;

import com.jw.server.model.OrderCancel;
import com.jw.server.model.OrderCreate;
import com.jw.server.model.OrderMatch;
import com.jw.server.service.OrderCancelRepository;
import com.jw.server.service.OrderCreateRepository;
import com.jw.server.service.OrderMatchRepository;
import com.jw.server.util.ByteUtil;

/**
 * 订单数据解析入库
 * 
 * @author xfm 2018年5月17日
 */
@RestController
@RequestMapping("/order")
public class OrderDataController {

	@Autowired
	OrderMatchRepository orderMatchRepository;

	@Autowired
	OrderCancelRepository orderCancelRepository;

	@Autowired
	OrderCreateRepository orderCreateRepository;

	@PostMapping("/create")
	public String orderCreate(@RequestBody byte[] content) {
		byte[] b = null;
		// 解压数据
		b = ByteUtil.decompress(content);
		try {
			// 拿到第一层
			OTIpcList b1 = OTIpcDef.OTIpcList.parseFrom(b);
			ListIterator<OTIpcDef.OTIpc> iter = b1.getOtpicList()
					.listIterator();
			List<OrderCreate> basepay = null;
			while (iter.hasNext()) {
				OTIpcDef.OTIpc o = iter.next();
				List<OTIpcDef.OrderCreate> li = o.getOrderCreateList();
				if (li != null && li.size() > 0) {
					Iterator<OTIpcDef.OrderCreate> l = li.iterator();
					basepay = new ArrayList<OrderCreate>();
					while (l.hasNext()) {
						OTIpcDef.OrderCreate base = l.next();
						basepay.add(OrderCreate.convertPojo(base));
					}
				}
			}
			// 将数据存入数据库 basepay
			System.out.println("收orderCreate表的数据条数："+basepay.size());
			orderCreateRepository.save(basepay);

		} catch (Exception e) {
			e.printStackTrace();
			return "400";
		}
		return "200";
	}

	@PostMapping("/match")
	public String orderMatch(@RequestBody byte[] content) {
		byte[] b = null;
		// 解压数据
		b = ByteUtil.decompress(content);
		try {
			// 拿到第一层
			OTIpcList b1 = OTIpcDef.OTIpcList.parseFrom(b);
			ListIterator<OTIpcDef.OTIpc> iter = b1.getOtpicList()
					.listIterator();
			List<OrderMatch> basepay = null;
			while (iter.hasNext()) {
				OTIpcDef.OTIpc o = iter.next();
				List<OTIpcDef.OrderMatch> li = o.getOrderMatchList();
				if (li != null && li.size() > 0) {
					Iterator<OTIpcDef.OrderMatch> l = li.iterator();
					basepay = new ArrayList<OrderMatch>();
					while (l.hasNext()) {
						OTIpcDef.OrderMatch base = l.next();
						basepay.add(OrderMatch.convertPojo(base));
					}
				}
			}
			// 将数据存入数据库 basepay
			System.out.println("收orderMatch表的数据条数："+basepay.size());
			orderMatchRepository.save(basepay);

		} catch (Exception e) {
			e.printStackTrace();
			return "400";
		}
		return "200";
	}

	@PostMapping("/cancel")
	public String orderCancel(@RequestBody byte[] content) {
		byte[] b = null;
		// 解压数据
		b = ByteUtil.decompress(content);
		try {
			// 拿到第一层
			OTIpcList b1 = OTIpcDef.OTIpcList.parseFrom(b);
			ListIterator<OTIpcDef.OTIpc> iter = b1.getOtpicList()
					.listIterator();
			List<OrderCancel> basepay = null;
			while (iter.hasNext()) {
				OTIpcDef.OTIpc o = iter.next();
				List<OTIpcDef.OrderCancel> li = o.getOrderCancelList();
				if (li != null && li.size() > 0) {
					Iterator<OTIpcDef.OrderCancel> l = li.iterator();
					basepay = new ArrayList<OrderCancel>();
					while (l.hasNext()) {
						OTIpcDef.OrderCancel base = l.next();
						basepay.add(OrderCancel.convertPojo(base));
					}
				}
			}
			// 将数据存入数据库 basepay
			System.out.println("收orderCancel表的数据条数："+basepay.size());
			orderCancelRepository.save(basepay);

		} catch (Exception e) {
			e.printStackTrace();
			return "400";
		}
		return "200";
	}

}
